package com.vcredit.creditcat.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vcredit.framework.annotation.Log;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IResourceManger;
import com.vcredit.creditcat.dto.resource.ResourceDto;

/**
 * @author chengchao
 *
 */
@Controller
@RequestMapping("/resourceImage")
public class ResourceImageController extends BaseCreditCatController {
	@Reference(version = "1.0", group = "ftp")
	private IResourceManger resourceManager;

	@Value("${project.whileList}")
	private String whileListStr;

	private static Logger logger = LoggerFactory.getLogger(ResourceImageController.class);

	@Log
	@RequestMapping(value = "/{resourceCode}", method = RequestMethod.GET)
	public void getImageResource(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("resourceCode") String resourceCode) throws Exception {

//		// 白名单验证
		String ip = getIpAddress(request);
		logger.info("ip:"+ip);
//		logger.info("whileList:"+whileListStr);
//		boolean bs = false;
//		if(StringUtils.isNotBlank(whileListStr)){
//			String[] ips=whileListStr.split(",");
//			for (String validIp:ips){
//				if(ip.matches(validIp)){
//					bs = true;
//					break;
//				}
//			}
//		}else{
//			bs = true;
//		}
//		if (bs) {
			//输出图片
			ResourceDto resourceDto = resourceManager.download(resourceCode);
			response.setHeader("Content-Type", resourceDto.getResourceType().getMediaType());
			response.setStatus(HttpStatus.SC_OK);
			response.getOutputStream().write(resourceDto.getResourceContent());
//		}
	}

	public final static String getIpAddress(HttpServletRequest request) throws Exception {
		// 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

		String ip = request.getHeader("X-Forwarded-For");

		System.out.println("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");

				System.out.println("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);

			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");

				System.out.println("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);

			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");

				System.out.println("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);

			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");

				System.out.println("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);

			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();

				System.out.println("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);

			}
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; index++) {
				String strIp = (String) ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip;
	}

}
